package com.incar.chuxing.baiduMap.MarkerOverlay;

import android.content.Context;
import android.graphics.Color;
import android.util.ArrayMap;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.PolygonOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.model.LatLng;
import com.incar.chuxing.bean.CarTrackBean;
import com.incar.chuxing.bean.CityBoundBean;
import com.incar.chuxing.utils.DensityUtils;
import com.incar.chuxing.utils.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by michaelpi on 2018/4/4.
 */

/*
 * 画线
 * */
public class BoundOverlay {

    private BaiduMap mBaiduMap;
    private Context mContext;
    public Polyline polygonLineCarTrack;
    public ArrayMap<String, Overlay> boundsAndCoverList = new ArrayMap<String, Overlay>();//保存所有围栏和cover，以及各个围栏的属性

    public BoundOverlay(Context mContext, BaiduMap mBaiduMap) {
        this.mContext = mContext;
        this.mBaiduMap = mBaiduMap;
    }


    //添加围栏

    public void addBounds(List<CityBoundBean.AttachmentBean.FenceAreaBean> allBounds, float curMapZoomLevel) {

        Logger.d("bounds", "添加了围栏bound");
        if (allBounds != null && allBounds.size() != 0) {
            //循环所有的围栏
            for (int j = 0; j < allBounds.size(); j++) {
                Logger.d("bounds", "围栏数" + allBounds.size());

                //添加围栏
                int showHideBoundLevel = allBounds.get(j).getHideLevel();//0：一直显示，正数：大于showHideLevel时显示,负数：小于等于showHideLevel时显示
                //需要显示的围栏
                if (allBounds.get(j).getStatus() == 1
                        && (showHideBoundLevel == 0 || (showHideBoundLevel > 0 && curMapZoomLevel > showHideBoundLevel) || (showHideBoundLevel < 0 && curMapZoomLevel <= -showHideBoundLevel))) {
                    //是已保存的围栏
                    if (boundsAndCoverList.containsKey(j + "bound") && null != boundsAndCoverList.get(j + "bound")) {
                        if (!boundsAndCoverList.get(j + "bound").isVisible()) {//已保存，但未显示，则显示
                            boundsAndCoverList.get(j + "bound").setVisible(true);
                        }
                    }
                    //是未保存的围栏
                    else {
                        List<LatLng> boundLatLngList = new ArrayList<>();
                        //生成单个围栏的数据
                        for (int i = 0; i < allBounds.get(j).getFenceScope().size(); i++) {
                            LatLng latLng = new LatLng(allBounds.get(j).getFenceScope().get(i).getLat(), allBounds.get(j).getFenceScope().get(i).getLng());
                            boundLatLngList.add(latLng);
                        }
                        LatLng latLng = new LatLng(allBounds.get(j).getFenceScope().get(0).getLat(), allBounds.get(j).getFenceScope().get(0).getLng());
                        boundLatLngList.add(latLng);
                        //添加到地图上

                        PolylineOptions polylineOptions = new PolylineOptions()
                                .points(boundLatLngList)
                                .width(DensityUtils.dp2px(mContext, allBounds.get(j).getStrokeWeight()))
                                .color(Color.parseColor(allBounds.get(j).getStrokeStyle()))
                                .dottedLine(allBounds.get(j).getIsDotted() == 1);

                        boundsAndCoverList.put(j + "bound", mBaiduMap.addOverlay(polylineOptions));
                   /*     boundsAndCoverList.put(j + "bound",
                                mBaiduMap.addOverlay(
                                        new PolygonOptions()
                                                .points(boundLatLngList)
                                                .stroke(new Stroke(DensityUtils.dp2px(mContext, allBounds.get(j).getStrokeWeight()), Color.parseColor(allBounds.get(j).getStrokeStyle())))
                                                .fillColor(0x00000000)
                                                .zIndex(1)));*/
                    }
                }
                //需要隐藏的围栏
                else {
                    //判断是不是已保存的围栏
                    //是已保存的围栏
                    Logger.d("bounds", "需要隐藏一个围栏" + j);
                    if (boundsAndCoverList.containsKey(j + "bound") && null != boundsAndCoverList.get(j + "bound")) {
                        if (boundsAndCoverList.get(j + "bound").isVisible()) {//已保存，但已显示,则隐藏
                            boundsAndCoverList.get(j + "bound").setVisible(false);
                        }
                    }
                }


                //添加Cover
                int showHideCoverLevel = allBounds.get(j).getFillZoom();//0：一直显示，正数：大于showHideLevel时显示,负数：小于等于showHideLevel时显示
                //需要显示的Cover
                if (showHideCoverLevel == 0 || (showHideCoverLevel > 0 && curMapZoomLevel > showHideCoverLevel) || (showHideCoverLevel < 0 && curMapZoomLevel <= -showHideCoverLevel)) {
                    //是已保存的Cover
                    if (boundsAndCoverList.containsKey(j + "cover") && null != boundsAndCoverList.get(j + "cover")) {
                        if (!boundsAndCoverList.get(j + "cover").isVisible()) {//已保存，但未显示，则显示
                            boundsAndCoverList.get(j + "cover").setVisible(true);
                        }
                    }
                    //是未保存的Cover
                    else {
                        List<LatLng> boundLatLngList = new ArrayList<>();
                        //生成单个围栏的数据
                        for (int i = 0; i < allBounds.get(j).getFenceScope().size(); i++) {
                            LatLng latLng = new LatLng(allBounds.get(j).getFenceScope().get(i).getLat(), allBounds.get(j).getFenceScope().get(i).getLng());
                            boundLatLngList.add(latLng);
                        }
                        //添加到地图上
                        Logger.d("colorfill", allBounds.get(j).getFillStyle());
                        boundsAndCoverList.put(j + "cover",
                                mBaiduMap.addOverlay(
                                        new PolygonOptions()
                                                .points(boundLatLngList)
                                                .stroke(new Stroke(0, 0x00000000))
                                                .fillColor(Color.parseColor(allBounds.get(j).getFillStyle()))
                                                .zIndex(1)));
                    }
                }
                //需要隐藏的Cover
                else {
                    //判断是不是已保存的Cover
                    //是已保存的Cover
                    if (boundsAndCoverList.containsKey(j + "cover") && null != boundsAndCoverList.get(j + "cover")) {
                        if (boundsAndCoverList.get(j + "cover").isVisible()) {//已保存，但已显示,则隐藏
                            boundsAndCoverList.get(j + "cover").setVisible(false);
                        }
                    }
                }


            }
        }


    }


    private List<LatLng> createTrackPoints(List<CarTrackBean.AttachmentBean> lineBeans) {
        List<LatLng> boundLstLngList = new ArrayList<>();
        for (int i = 0; i < lineBeans.size(); i++) {
            LatLng latLng = new LatLng(lineBeans.get(i).getLatitude(), lineBeans.get(i).getLongitude());
            boundLstLngList.add(latLng);
        }
        return boundLstLngList;
    }

    //寻车添加上辆车最后轨迹

    public void addCarTrack(List<CarTrackBean.AttachmentBean> lineBeans) {
        if (lineBeans != null && lineBeans.size() != 0) {
            polygonLineCarTrack = (Polyline) mBaiduMap.addOverlay(
                    new PolylineOptions()
                            .points(createTrackPoints(lineBeans))
                            .zIndex(6)
                            .width(6)
                            .color(0xFFFEA30B).zIndex(3)
            );
        }
    }


    public void clearData() {
        Logger.d("清除了围栏和所有marker");
        if (polygonLineCarTrack != null) {
            polygonLineCarTrack.remove();
        }
        boundsAndCoverList.clear();
    }

}
